package com.genloong.flycloud.filecenter.controller;

import cn.hutool.Hutool;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.genloong.flycloud.common.bean.VO.ResultVO;
import com.genloong.flycloud.filecenter.entity.BizFile;
import com.genloong.flycloud.filecenter.entity.SysConfig;
import com.genloong.flycloud.filecenter.message.MessageServer;
import com.genloong.flycloud.filecenter.service.BizFileService;
import com.genloong.flycloud.filecenter.service.SysConfigService;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (BizFile)表控制层
 *
 * @author makejava
 * @since 2020-08-04 14:06:11
 */
@ApiOperation(value = "文件中心接口")
@RestController
@RequestMapping("bizFile")
public class BizFileController {
    /**
     * 服务对象
     */
    @Resource
    private BizFileService bizFileService;
    @Resource
    private SysConfigService sysConfigService;
    @Resource
    private MessageServer  messageServer;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public BizFile selectOne(Integer id) {
        return this.bizFileService.getById(id);
    }
    @PostMapping("downloadFile")
    public ResultVO<JSONArray> downloadFile(@RequestBody List<BizFile> files){
        ResultVO resultVO = ResultVO.ResultVOFactory();
        JSONArray filesVO=new JSONArray();
        resultVO.setData(filesVO);
        //获取文件存储路径
        SysConfig file_save_path_obj = sysConfigService.getOne(new QueryWrapper<SysConfig>()
                .eq("config_name","file_save_path"));
        //获取文件显示路径
        SysConfig file_show_path_obj = sysConfigService.getOne(new QueryWrapper<SysConfig>()
                .eq("config_name", "file_show_path"));
        String show_path = file_show_path_obj.getConfigValue();

        if (files!=null&&files.size()>0){

            for (BizFile file : files) {
                if (StringUtils.isEmpty(file.getFilePath())||StringUtils.isEmpty(file.getFileType())){
                    continue;
                }

                String fileName = IdUtil.simpleUUID();
                    String s = fileName + file.getFileType();
                    file.setFileName(s);
                    String e = show_path + s;
                    BizFile clone = file.clone();
                    clone.setFilePath(e);
                    filesVO.add(e);
                    bizFileService.save(clone);
                    messageServer.send("selfData","file_center_dowaload_file", JSON.toJSONString(file));
            }
        }
        resultVO.success("正在保存...");
       return resultVO;
    }



}